package com.shop.www.dao.Impl;

import com.shop.www.dao.OrderDAO;
import com.shop.www.model.Order;
import com.shop.www.model.User;
import com.shop.www.ssm.BasicDao;

import java.util.List;

public class OrderDAOImpl extends BasicDao implements OrderDAO {
    @Override
    public void addorder(Order order) {
        String sql = ("insert into `order` values(0,?,?,?,?,?,?,?,?)");
        update(sql,order.getOrder_No(),order.getOrder_date(),order.getOrder_money(),order.getOrder_count(),order.getOrder_status(),order.getUser_ID(),order.getSeller_ID(),null);
        String sql2 = ("SELECT LAST_INSERT_ID()");
        Object order_IDobj = queryScalar(sql2);
        int order_ID = Integer.parseInt(order_IDobj.toString());
        order.setOrder_ID(order_ID);
    }

    @Override
    public List<Order> getorderbyuser(User user) {//根据用户获取所有订单
        String sql = ("SELECT * from `order` where user_ID = ? ORDER BY order_ID desc");
        List<Order>  orderList = querymany(sql,Order.class,user.getUser_ID());
        return orderList;
    }

    @Override
    public List getorderByseller(Integer user_ID) {
        String sql = ("SELECT * from `order` where seller_ID = ? ORDER BY order_ID desc");
        List<Order>  orderList = querymany(sql,Order.class,user_ID);
        return orderList;
    }

    @Override
    public void updateorder(Order order) {
        String sql = ("update `order` set order_status = ? ,courier_ID = ? where order_ID = ?");
        update(sql,order.getOrder_status(),order.getCourier_ID(),order.getOrder_ID());
    }

    @Override
    public List<Order> getorder(String order_status) {
        String sql = ("SELECT * from `order` where order_status like ? ORDER BY order_ID desc");
        List<Order> orderList =querymany(sql,Order.class,order_status);
        return orderList;
    }

    @Override
    public Order getorderByID(Integer order_ID) {
        String sql = ("select *  from `order` where order_ID =?");
        return (Order) querysingle(sql,Order.class,order_ID);
    }

    @Override
    public List<Order> getorderBycourier(Integer user_ID) {
        String sql = ("SELECT * from `order` where courier_ID = ? ORDER BY order_ID desc");
        List<Order> orderList = querymany(sql,Order.class,user_ID);
        return orderList;
    }

    @Override
    public void finishorder(Order order) {
        String sql = ("update `order` set order_status = ? where order_ID = ?");
        update(sql,order.getOrder_status(),order.getOrder_ID());
    }
}
